
package com.heimali.happyreader.core.net;

import com.heimali.happyreader.core.bean.Article;
import com.heimali.happyreader.core.bean.Intro;
import com.heimali.happyreader.utils.HttpUtils;

import java.net.URI;
import java.util.List;

public class ArticleApi {
    public static String Tag = "ArticleApi";

    private final String PROTOCOL = "http";
    private final String HOST = "os.heimali.com";
    private final int PORT = 80;

    private final String DETAIL_PATH = "/HappyReader/mobiArticleV3.php";
    private final String LIST_PATH = "/HappyReader/mobiListV3.php";
    private final String LASTEST_LIST_PATH = "/HappyReader/mobiListIncV3.php";

    public List<Intro> getList(int minID, int count) {
        String query = HttpUtils.buildQuery(new String[] {
                "client_min_id", String.valueOf(minID)
        }, new String[] {
                "count", String.valueOf(count)
        });
        String content = Request.get(buildURI(LIST_PATH, query));
        return JsonParser.listParser(content);
    }

    public List<Intro> getLastestList(int maxID) {
        String query = HttpUtils.buildQuery(new String[] {
                "client_max_id", String.valueOf(maxID)
        });
        String content = Request.get(buildURI(LASTEST_LIST_PATH, query));
        return JsonParser.listParser(content);
    }

    public Article getDetail(int id) {
        String query = HttpUtils.buildQuery(new String[] {
                "aid", String.valueOf(id)
        });
        String content = Request.get(buildURI(DETAIL_PATH, query));
        return JsonParser.articleParser(content);
    }

    private URI buildURI(String uri, String query) {
        return HttpUtils.buildURI(PROTOCOL, HOST, PORT, uri, query);
    }
}
